/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gerenciadorfinanceiro.converter;

import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.InitialContext;
import org.gerenciadorfinanceiro.ejb.entity.OutcomeSource;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSourceSessionLocal;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSourceSessionRemote;

/**
 *
 * @author eduardo
 */
public class OutcomeSourceConverter implements Converter {

    public final static String CONVERTER_ID = "org.gerenciadorfinanceiro.ejb.entity.OutcomeSource"; //path dos beans da classe Estado
    @EJB
    private OutcomeSourceSessionLocal sourceBean;

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if
         (value != null && (!"".equals(value))) {
            return getSourceBean().getSourceByName(value);
        } else {
            return null;
        }
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value != null) {
            return ((OutcomeSource) value).getDescriptionSource();
        } else {
            return "";
        }
    }

    /**
     * @return the sourceBean
     */
    public OutcomeSourceSessionLocal getSourceBean() {
        try {
            return (OutcomeSourceSessionRemote) new InitialContext().lookup("org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSourceSessionRemote");
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}
